package com.zwh.algorithm.leetcode.medium;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

public class LeetCode772 {
    /**
     * 2023-08-03 删除注释
     * todo 给一个 C++ 程序，删除程序中的注释。这个程序source是一个数组，其中source[i]表示第 i 行源码。 这表示每行源码由 '\n' 分隔。
     * @param args
     */
    public static void main(String[] args) {
//        System.out.println(removeComments(new String[]{"a/*comment","line","more_comment*/b"}));
        System.out.println(removeComments(new String[]{"/*Test program */","int main()","{", "  // variable declaration ", "int a, b, c;", "/* This is a test", "   multiline  ", "   comment for ", "   testing */", "a = b + c;", "}"}));

    }
    public static List<String> removeComments(String[] source) {
        StringBuilder a = new StringBuilder();
        for (String s : source) {
            if (s.indexOf("//") != -1){
                s = s.substring(0,s.indexOf("//"));
            }
            if ( s != ""){
                if (s.length() >=2 && s.endsWith("*/")){
                    a.append( s);
                }else{
                    a.append( s+"\n");
                }
            }
        }
        System.out.println(a);
        List<String> list = new ArrayList<>();
        int i1 = a.indexOf("/*");
        while (i1 != -1){
            int i = a.indexOf("*/");
            if (i ==i1+1){
                i = a.substring(i+1).indexOf("*/");
            }
            a.delete(i1,i +2);
            i1 = a.indexOf("/*");
        }
        System.out.println(a);
        int i2 = a.indexOf("\n");
        while (i2 != -1){
            list.add(a.substring(0,i2));
            a = a.delete(0,i2 +1);
            i2 = a.indexOf("\n");
        }
        return list;
    }

    public static List<String> removeComments1(String[] source) {
        List<String> res = new ArrayList<String>();
        StringBuilder newLine = new StringBuilder();
        boolean inBlock = false;
        for (String line : source) {
            for (int i = 0; i < line.length(); i++) {
                if (inBlock) {
                    if (i + 1 < line.length() && line.charAt(i) == '*' && line.charAt(i + 1) == '/') {
                        inBlock = false;
                        i++;
                    }
                } else {
                    if (i + 1 < line.length() && line.charAt(i) == '/' && line.charAt(i + 1) == '*') {
                        inBlock = true;
                        i++;
                    } else if (i + 1 < line.length() && line.charAt(i) == '/' && line.charAt(i + 1) == '/') {
                        break;
                    } else {
                        newLine.append(line.charAt(i));
                    }
                }
            }
            if (!inBlock && newLine.length() > 0) {
                res.add(newLine.toString());
                newLine.setLength(0);
            }
        }
        return res;
    }

    /**
     * class Solution:
     *     def removeComments(self, source: List[str]) -> List[str]:
     *         # 匹配所有 // 和 /* * /，后者用非贪婪模式。将所有匹配结果替换成空串。最后移除多余空行。
     *         return list(filter(None, re.sub('//.*|/\*(.|\n)*?\* /', '', '\n'.join(source)).split('\n')))
     */
}
